import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';

class DatePickerDemo extends StatefulWidget {
  DatePickerDemo({Key? key}) : super(key: key);

  @override
  State<DatePickerDemo> createState() => _DatePickerDemoState();
}

class _DatePickerDemoState extends State<DatePickerDemo> {
  var now = new DateTime.now();
  DateTime _nowDate = new DateTime.now();
  var _nowTime = TimeOfDay(hour: 12, minute: 20);
  _showDatePicker() async {
    // showDatePicker(
    //         context: context,
    //         initialDate: _nowDate,
    //         firstDate: DateTime(1980),
    //         lastDate: DateTime(2100))
    //     .then((res) {
    //   print(res);
    // });
    var res = await showDatePicker(
        context: context,
        initialDate: _nowDate,
        firstDate: DateTime(1980),
        locale: Locale('zh'),
        lastDate: DateTime(2100));
    print(res);
    setState(() {
      this._nowDate = res as DateTime;
    });
  }

  _showTimePicker() async {
    // showTimePicker(
    //         context: context,
    //         initialTime: _nowTime,
    //
    //     .then((res) {
    //   print(res);
    // });
    var res = await showTimePicker(context: context, initialTime: _nowTime);
    print(res);
    setState(() {
      this._nowTime = res as TimeOfDay;
    });
  }

  @override
  void initState() {
    super.initState();
    // print(now);2022-04-23 13:56:51.906168
    // print(now.millisecondsSinceEpoch);1650693471287
    print(DateTime.fromMillisecondsSinceEpoch(1650693471287));
    print(formatDate(DateTime.now(), [yyyy, '年', mm, '月', dd]));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("DatePicker"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              InkWell(
                child:
                    Row(mainAxisAlignment: MainAxisAlignment.center, children: <
                        Widget>[
                  Text(
                      "${formatDate(this._nowDate, [yyyy, '年', mm, '月', dd])}"),
                  Icon(Icons.arrow_drop_down)
                ]),
                onTap: _showDatePicker,
              ),
              InkWell(
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text("${_nowTime.format(context)}"),
                      Icon(Icons.arrow_drop_down)
                    ]),
                onTap: _showTimePicker,
              ),
            ],
          )
        ],
      ),
    );
  }
}
